/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */


#include <QTreeWidgetItem>

#include "goongress.h"

//

void QTgoongress::popola()
{
     QSettings menuSettings(CONFIG_FORMAT, QSettings::UserScope,
                            COMPANY_NAME, APPLICATION_NAME);
     bool firstRun = menuSettings.value("firstRun", true).toBool();
     if (firstRun) {
         initTree(&menuSettings);
     }
     readTreeFromSettings(&menuSettings);
}

void QTgoongress::initTree(QSettings *settings)
{
    QSettings initMenuSettings(":/resources/conf/medusa.conf",CONFIG_FORMAT );
     int size = initMenuSettings.beginReadArray("menu");
     settings->beginWriteArray("menu");
     for (int i = 0; i < size; i++) {
         initMenuSettings.setArrayIndex(i);
         settings->setArrayIndex(i);
         settings->setValue("title", initMenuSettings.value("title").toString());
         int menuSize = initMenuSettings.beginReadArray("menuItem");
         settings->beginWriteArray("menuItem");
         for (int j = 0; j < menuSize; j++) {
             initMenuSettings.setArrayIndex(j);
             settings->setArrayIndex(j);
             settings->setValue("item", initMenuSettings.value("item").toString());
             settings->setValue("url", initMenuSettings.value("url").toString());
             settings->setValue("icon", initMenuSettings.value("icon").toString());
         }
         initMenuSettings.endArray();
         settings->endArray();
     }
     initMenuSettings.endArray();
     settings->endArray();
     settings->setValue("firstRun", false);
     settings->sync();
}


void QTgoongress::readTreeFromSettings(QSettings *menuSettings)
{
    const QString *path = new QString(":/resources/icons/folder.png");
     int size = menuSettings->beginReadArray("menu");
     for (int i = 0; i < size; i++) {
         menuSettings->setArrayIndex(i);
         QTreeWidgetItem *menu = new QTreeWidgetItem(&box);
         menu->setText(0, menuSettings->value("title").toString());
         menu->setExpanded(true);
         int menuSize = menuSettings->beginReadArray("menuItem");
         for (int j = 0; j < menuSize; j++) {
             menuSettings->setArrayIndex(j);
             QTreeWidgetItem *item = new QTreeWidgetItem(menu);
             item->setText(0, menuSettings->value("item").toString());
             item->setText(1, menuSettings->value("url").toString());
             item->setIcon(0, QIcon(menuSettings->value("icon", path->toStdString().c_str()).toString()));
         }
         menuSettings->endArray();
     }
     menuSettings->endArray();
}
